<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="现象：近来，发现Asterisk SIP服务经常占用CPU 100%以上，而且都在服务启动1个小时后出现，但重启服务后就恢复正常 初步分析：通过tshark对网络发包测试，并未发现有明显变化，因此不是请求量变大引发的CPU紧张（从重启服务恢复正常也可以证实），因此可以确定是服务内部的问题，得益于 《Why does Asterisk consume 100% CPU?》 这篇文章，我对asteri">
<meta property="og:type" content="article">
<meta property="og:title" content="分析并解决Asterisk CPU占用过高问题">
<meta property="og:url" content="http://smallmuou.gitee.io/blog/2018/08/17/2018-08-17-分析并解决Asterisk CPU占用过高问题/index.html">
<meta property="og:site_name" content="smallmuou&#39;s blog">
<meta property="og:description" content="现象：近来，发现Asterisk SIP服务经常占用CPU 100%以上，而且都在服务启动1个小时后出现，但重启服务后就恢复正常 初步分析：通过tshark对网络发包测试，并未发现有明显变化，因此不是请求量变大引发的CPU紧张（从重启服务恢复正常也可以证实），因此可以确定是服务内部的问题，得益于 《Why does Asterisk consume 100% CPU?》 这篇文章，我对asteri">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-04-16T01:15:21.941Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="分析并解决Asterisk CPU占用过高问题">
<meta name="twitter:description" content="现象：近来，发现Asterisk SIP服务经常占用CPU 100%以上，而且都在服务启动1个小时后出现，但重启服务后就恢复正常 初步分析：通过tshark对网络发包测试，并未发现有明显变化，因此不是请求量变大引发的CPU紧张（从重启服务恢复正常也可以证实），因此可以确定是服务内部的问题，得益于 《Why does Asterisk consume 100% CPU?》 这篇文章，我对asteri">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://smallmuou.gitee.io/blog/2018/08/17/2018-08-17-分析并解决Asterisk CPU占用过高问题/"/>





  <title>分析并解决Asterisk CPU占用过高问题 | smallmuou's blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">smallmuou's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"><a href="https://github.com/smallmuou">https://github.com/smallmuou</a></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/blog/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2018/08/17/2018-08-17-分析并解决Asterisk CPU占用过高问题/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">分析并解决Asterisk CPU占用过高问题</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-17T00:00:00+08:00">
                2018-08-17
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/音视频/" itemprop="url" rel="index">
                    <span itemprop="name">音视频</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>现象：近来，发现Asterisk SIP服务经常占用CPU 100%以上，而且都在服务启动1个小时后出现，但重启服务后就恢复正常</p>
<p>初步分析：通过tshark对网络发包测试，并未发现有明显变化，因此不是请求量变大引发的CPU紧张（从重启服务恢复正常也可以证实），因此可以确定是服务内部的问题，得益于 <a href="https://moythreads.com/wordpress/2009/05/06/why-does-asterisk-consume-100-cpu/" target="_blank" rel="noopener">《Why does Asterisk consume 100% CPU?》</a> 这篇文章，我对asterisk服务进行了深度剖析，过程如下</p>
<h3 id="步骤1-获取哪个线程（Light-Weight-Process）占用CPU最高"><a href="#步骤1-获取哪个线程（Light-Weight-Process）占用CPU最高" class="headerlink" title="步骤1. 获取哪个线程（Light Weight Process）占用CPU最高"></a>步骤1. 获取哪个线程（Light Weight Process）占用CPU最高</h3><p>ps -LlFm -p <code>pidof asterisk</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">F S UID        PID  PPID   LWP  C NLWP PRI  NI ADDR SZ WCHAN    RSS PSR STIME TTY          TIME CMD</span><br><span class="line">4 - root     13090 13088     - 25   47   -   - - 522547 -     573636  - 08:43 pts/18   00:07:28 /usr/sbin/asterisk -f -vvvg -c</span><br><span class="line">4 S root         -     - 13090  0    -  80   0 -     - poll_s     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13091  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13092  0    -  80   0 -     - futex_     -   0 08:43 -        00:00:17 -</span><br><span class="line">1 R root         -     - 13093  3    -  80   0 -     - -          -   0 08:43 -        00:01:01 -</span><br><span class="line">1 S root         -     - 13094  1    -  80   0 -     - futex_     -   1 08:43 -        00:00:19 -</span><br><span class="line">1 S root         -     - 13096  0    -  80   0 -     - poll_s     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13098  0    -  80   0 -     - futex_     -   0 08:43 -        00:00:02 -</span><br><span class="line">1 S root         -     - 13100  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13101  0    -  80   0 -     - futex_     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13102  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13103  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13104  2    -  80   0 -     - futex_     -   1 08:43 -        00:00:48 -</span><br><span class="line">1 S root         -     - 13105  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13106  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13107  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13108  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13109  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13110  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13111  0    -  80   0 -     - futex_     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13112  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13113  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13114  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13115  0    -  80   0 -     - inet_c     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13116  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13117  0    -  80   0 -     - poll_s     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13118  0    -  80   0 -     - futex_     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13119  0    -  80   0 -     - poll_s     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 R root         -     - 13120 16    -  80   0 -     - -          -   0 08:43 -        00:04:52 -</span><br><span class="line">1 S root         -     - 13121  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13122  0    -  80   0 -     - futex_     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13123  0    -  80   0 -     - poll_s     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13124  0    -  80   0 -     - hrtime     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13125  0    -  80   0 -     - futex_     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13126  0    -  80   0 -     - poll_s     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13127  0    -  80   0 -     - futex_     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13128  0    -  80   0 -     - poll_s     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13129  0    -  80   0 -     - hrtime     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13130  0    -  80   0 -     - hrtime     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13131  0    -  80   0 -     - poll_s     -   0 08:43 -        00:00:00 -</span><br></pre></td></tr></table></figure>
<p>其中C列表示线程占用CPU的百分比（这个值是自服务启动到目前的平均值，而top命令是实时，因此与top会不一致），从以上我们可以确定是13120线程占用最高，接下来我们要分析13120到底执行了什么</p>
<h3 id="步骤2-获取具体线程执行堆栈信息"><a href="#步骤2-获取具体线程执行堆栈信息" class="headerlink" title="步骤2. 获取具体线程执行堆栈信息"></a>步骤2. 获取具体线程执行堆栈信息</h3><p>pstack <code>pidof asterisk</code><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">Thread 21 (Thread 0x7fecd94ea700 (LWP 13120)):</span><br><span class="line">#0  0x00000000005aee3f in tps_cmp_cb ()</span><br><span class="line">#1  0x00000000004580a5 in internal_ao2_traverse ()</span><br><span class="line">#2  0x0000000000458b51 in __ao2_find ()</span><br><span class="line">#3  0x00000000005aef0d in ast_taskprocessor_create_with_listener ()</span><br><span class="line">#4  0x00000000005b4f1e in ast_threadpool_serializer ()</span><br><span class="line">#5  0x000000000059acc1 in internal_stasis_subscribe.clone.1 ()</span><br><span class="line">#6  0x00000000005a6168 in stasis_message_router_create_internal ()</span><br><span class="line">#7  0x00000000004ef227 in endpoint_internal_create ()</span><br><span class="line">#8  0x00007fecf0ec76c6 in build_peer () from /usr/lib/asterisk/modules/chan_sip.so</span><br><span class="line">#9  0x00007fecf0ed5a48 in realtime_peer () from /usr/lib/asterisk/modules/chan_sip.so</span><br><span class="line">#10 0x00007fecf0ed6646 in sip_find_peer_full () from /usr/lib/asterisk/modules/chan_sip.so</span><br><span class="line">#11 0x00007fecf0ed66b7 in sip_find_peer () from /usr/lib/asterisk/modules/chan_sip.so</span><br><span class="line">#12 0x00007fecf0f00aa7 in register_verify () from /usr/lib/asterisk/modules/chan_sip.so</span><br><span class="line">#13 0x00007fecf0f02cb5 in handle_request_register () from /usr/lib/asterisk/modules/chan_sip.so</span><br><span class="line">#14 0x00007fecf0f0d166 in handle_incoming () from /usr/lib/asterisk/modules/chan_sip.so</span><br><span class="line">#15 0x00007fecf0f0ed84 in handle_request_do () from /usr/lib/asterisk/modules/chan_sip.so</span><br><span class="line">#16 0x00007fecf0f10786 in sipsock_read () from /usr/lib/asterisk/modules/chan_sip.so</span><br><span class="line">#17 0x0000000000514b52 in ast_io_wait ()</span><br><span class="line">#18 0x00007fecf0ed4c6b in do_monitor () from /usr/lib/asterisk/modules/chan_sip.so</span><br></pre></td></tr></table></figure></p>
<p>从以上我们可以发现与创建taskprocessors有关，于是我们看下了taskprocessors数量</p>
<p>asterisk -rx ‘core show taskprocessors’</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">5d07f1f8-56be-4456-ac64-08dd619b09a9                   2            0            1</span><br><span class="line">88498730-b675-433d-b71c-eaa51fde68b6                   2            0            1</span><br><span class="line">eaabf337-81d5-4411-a0dc-a05f20071c7c                   2            0            1</span><br><span class="line">9dc17b55-f7cb-4ff4-bbcd-44033893eb4f                   2            0            1</span><br><span class="line">afa4cfff-4cb9-451b-a5d1-7ace2e4a9040                   2            0            0</span><br><span class="line">67729e31-8e78-423b-b5d5-dd746190f374                   2            0            0</span><br><span class="line">b9b5e393-f016-4d09-b8fe-03d7633ef174                   2            0            0</span><br><span class="line">    +---------------------+-----------------+------------+-------------+</span><br><span class="line">    55585 taskprocessors</span><br></pre></td></tr></table></figure>
<p>通过以上推测，应该是大量的taskprocessors占用的CPU资源，而且taskprocessors会不停增加</p>
<h3 id="步骤3：结合代码，确认原因"><a href="#步骤3：结合代码，确认原因" class="headerlink" title="步骤3：结合代码，确认原因"></a>步骤3：结合代码，确认原因</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">struct ast_taskprocessor *<span class="title">ast_taskprocessor_create_with_listener</span><span class="params">(<span class="keyword">const</span> <span class="keyword">char</span> *name, struct ast_taskprocessor_listener *listener)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">ast_taskprocessor</span> *<span class="title">p</span> = <span class="title">ao2_find</span>(<span class="title">tps_singletons</span>, <span class="title">name</span>, <span class="title">OBJ_KEY</span>);</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (p) &#123;</span><br><span class="line">        ast_taskprocessor_unreference(p);</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> __allocate_taskprocessor(name, listener);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>ao2_find是根据name从任务队列中查找，一旦这个队列非常大，那CPU需要不停比较，从而会占用大量CPU资源，至此，问题基本确定了；接下来就要进入苦活阶段</p>
<h3 id="步骤4：解决代码BUG"><a href="#步骤4：解决代码BUG" class="headerlink" title="步骤4：解决代码BUG"></a>步骤4：解决代码BUG</h3><p>通过分析代码发现如下关系： peer-&gt;endpoint-&gt;route-&gt;subscription-&gt;mailbox</p>
<p>mailbox就是”struct ast_taskprocessor”即任务对象，因此可以推断应该是这个依赖链中有对象内存泄露了导致mailbox没有从任务队列中移除，通过一级一级分析，发现是peer内存泄露了，看如下代码</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span> (!strcasecmp(curi, <span class="string">"*"</span>) || !expire) &#123; <span class="comment">/* Unregister this peer */</span></span><br><span class="line">    <span class="comment">/* This means remove all registrations and return OK */</span></span><br><span class="line">    struct sip_peer* peer_db = sip_find_peer_by_db(peer-&gt;peername, <span class="literal">NULL</span>, TRUE, FINDPEERS, FALSE, <span class="number">0</span>);</span><br><span class="line">    <span class="comment">//ast_log(LOG_WARNING, "unregister found name%s\n",peer-&gt;peername);</span></span><br><span class="line">    <span class="keyword">if</span>(peer_db!=<span class="literal">NULL</span> &amp;&amp; peer-&gt;uuid!=<span class="literal">NULL</span> &amp;&amp; peer_db-&gt;uuid!=<span class="literal">NULL</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//ast_log(LOG_WARNING, "db_uuid%s,peer_uuid%s\n", peer_db-&gt;uuid,peer-&gt;uuid);</span></span><br><span class="line">        <span class="keyword">if</span>(<span class="built_in">strcmp</span>(peer-&gt;uuid,peer_db-&gt;uuid) == <span class="number">0</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            AST_SCHED_DEL_UNREF(sched, peer-&gt;expire,</span><br><span class="line">                sip_unref_peer(peer, <span class="string">"remove register expire ref"</span>));</span><br><span class="line">            ast_verb(<span class="number">3</span>, <span class="string">"Unregistered SIP '%s'\n"</span>, peer-&gt;name);</span><br><span class="line">            expire_register(sip_ref_peer(peer,<span class="string">"add ref for explicit expire_register"</span>));</span><br><span class="line">            <span class="keyword">return</span> PARSE_REGISTER_UPDATE;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">        &#123;</span><br><span class="line">            ast_log(LOG_WARNING, <span class="string">"Invalid unregister db_uuid%s,but peer_uuid%s\n"</span>, peer_db-&gt;uuid,peer-&gt;uuid);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>peer_db是sip_find_peer_by_db得到的，而sip_find_peer_by_db是有’加引用’的，因此在没用peer_db时需要执行’去引用’，因此此处需要加sip_unref_peer(peer_db, “remove”)</p>
<p>当然代码中还有其他地方也是类似，这里不一一列出，改完所有泄露，重新编译运行发现taskprocessors不会不断增加，维持在30个以内，通过几个小时的运行，CPU没有增高，此问题得到的最终解决。</p>
<h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><p>【授之以鱼，不如授之以渔】以上只是一种CPU过高的原因，各位很可能遇到的问题不一样，但都可以借鉴上面步骤对问题进行排查。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/14/2018-08-14-lua调用c/" rel="next" title="lua调用c">
                <i class="fa fa-chevron-left"></i> lua调用c
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/20/2018-08-20-微信扫一扫自定义二维码实现支付/" rel="prev" title="微信扫一扫自定义二维码实现支付">
                微信扫一扫自定义二维码实现支付 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">smallmuou</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">197</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">12</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/smallmuou" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-globe"></i>GitHub</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#步骤1-获取哪个线程（Light-Weight-Process）占用CPU最高"><span class="nav-number">1.</span> <span class="nav-text">步骤1. 获取哪个线程（Light Weight Process）占用CPU最高</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#步骤2-获取具体线程执行堆栈信息"><span class="nav-number">2.</span> <span class="nav-text">步骤2. 获取具体线程执行堆栈信息</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#步骤3：结合代码，确认原因"><span class="nav-number">3.</span> <span class="nav-text">步骤3：结合代码，确认原因</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#步骤4：解决代码BUG"><span class="nav-number">4.</span> <span class="nav-text">步骤4：解决代码BUG</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#总结"><span class="nav-number">5.</span> <span class="nav-text">总结</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">smallmuou</span>

  
</div>

    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

    <span id="busuanzi_container_site_pv">总访问量<span id="busuanzi_value_site_pv"></span>次</span>
    <span class="post-meta-divider">|</span>



  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
